//
//  RegulationViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/7/29.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "RegulationViewController.h"
#import "DZViewCell.h"
@interface RegulationViewController ()
@property (nonatomic, assign)int page;
@end

@implementation RegulationViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"政策法规";
    self.tableView.rowHeight = 100;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"DZViewCell" bundle:nil] forCellReuseIdentifier:@"DZViewCell1"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        [self getdata:1];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self getdata:self.page];
    }];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.dataArray removeAllObjects];
    self.page = 1;
    [self getdata:self.page];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DZViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DZViewCell1" forIndexPath:indexPath];
    DangModel *model = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseImgUrl,model.thumbnail]] placeholderImage:[UIImage imageNamed:@"zhengce"]];
    cell.titleLabel.text = model.title;
    cell.timeLabel.text = [self ConvertStrToTime:model.create_time];
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DangModel *model = self.dataArray[indexPath.row];
    WebViewController *webVC = [[WebViewController alloc]init];
    
    
    
    webVC.noticeID = model.id;
    webVC.titleStr = @"政策法规";
    
    
    [self.navigationController pushViewController:webVC animated:YES];
    
}
//时间戳变为格式时间
- (NSString *)ConvertStrToTime:(NSString *)timeStr

{
    
    long long time=[timeStr longLongValue];
    //    如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //    long long time=[timeStr longLongValue] / 1000;
    
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString*timeString=[formatter stringFromDate:date];
    
    return timeString;
    
}


-(void)getdata:(int)page{
    WS(weakSelf);
    [[ToastManager shardInstance]showSystemProgress:self.view];
    [[ApiManager ShardInstance]noticeListParentid:@"1" page:page resultBlock:^(NSDictionary *data, NSError *error) {
        BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
        if (model.code == 1) {
            [[ToastManager shardInstance]hideProgress:self.view];
            NSArray *array = [DangModel mj_objectArrayWithKeyValuesArray:data[@"rs"][@"data"]];
            
            weakSelf.tableView.mj_footer.alpha = 0.0;
            [self.dataArray addObjectsFromArray:array];
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            weakSelf.page ++;
        }
    }];
}

@end
